package com.epcs.controller.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.EmailBO;
import com.epcs.model.bo.SettingBO;
import com.epcs.model.dao.EventCategoryDAO;
import com.epcs.model.dao.FollowingDAO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Email;
import com.epcs.model.dto.Eventcategory;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.Notification;
import com.epcs.model.dto.Setting;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class SettingAction extends ActionSupport implements SessionAware, ServletRequestAware  {

	// ---------- input fields ----------//
    private boolean isAutoSync;
    private Integer syncAfter;
    private int emailId;
    private String ajaxResult;
    
	
    private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;
	

	// ----- Get setting -----//
	public String getSetting() {
		
		String result = "Success";
		// get user session
		Account account = (Account) sessionMap.get("USER_INFO");

		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			// get username
			String username = account.getUsername();
			// get setting		
			Setting setting = new SettingBO().getSetting(account);			
			request.setAttribute("SETTING", setting);
			
			// get Email list
			EmailBO emailBO = new EmailBO();
			List<Email> emailList = emailBO.getAllEmail(account);
			request.setAttribute("EMAIL_LIST", emailList);	
			
			
		} else {
			// neu chua login thi login
			return "Unauthorized";
		}

		return result;
	}
	
	// ----------Action method -----------//
	// ----- Save setting -----//
	public String saveSetting() {
		System.out.println("server sessiion = " + sessionMap);
		String resultString = "Fail";
		// get user session
		Account account =  (Account) sessionMap.get("USER_INFO");

		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			// get setting
			Setting setting = new SettingBO().getSetting(account);
			setting.setIsAutoSync(isAutoSync);
			setting.setSyncAfter(syncAfter);
			
			// save change
			boolean result = new SettingBO().saveChange(setting);
			if(result){
				resultString = "Success";
			} else {
				request.setAttribute("WARNING_MESSAGE", "Bạn chưa đăng ký email để đồng bộ!");
			}
		} else {
			// neu chua login thi login
			return "Unauthorized";
		}

		return resultString;
	}
	
	// ----- Delete email in setting -----//
	public String removeEmail() {
		// get user session
		Account account =  (Account) sessionMap.get("USER_INFO");
		EmailBO emailBO = new EmailBO();
		Email email = emailBO.getEmail(emailId);
		
		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			
			// kiểm tra xem account đó có thuộc user đó ko 
			if (email.getAccount().getUsername().equals(account.getUsername())){
				// nếu đúng user hiện tại thì mới cho xóa
				boolean result = emailBO.deleteEmail(email);
				if(result){
					ajaxResult = "Success";
				} else {
					ajaxResult = "Fail";
				}
			}			
		} else {
			// neu chua login thi login
			ajaxResult = "Unauthorized";
		}
		
		return Action.SUCCESS;
	}

	
	// -----------Getter and Setter -----//
	public boolean getIsAutoSync() {
		return isAutoSync;
	}

	public void setIsAutoSync(boolean isAutoSync) {
		this.isAutoSync = isAutoSync;
	}

	public Integer getSyncAfter() {
		return syncAfter;
	}

	public void setSyncAfter(Integer syncAfter) {
		this.syncAfter = syncAfter;
	}

	public int getEmailId() {
		return emailId;
	}

	public void setEmailId(int emailId) {
		this.emailId = emailId;
	}

	public String getAjaxResult() {
		return ajaxResult;
	}

	public void setAjaxResult(String ajaxResult) {
		this.ajaxResult = ajaxResult;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
		
	}



}
